package com.demo.controller;


import com.sun.javafx.scene.control.skin.Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;


/**
 * 方法说明: 用于excel列的计算
 * @version v1.0
 * @author 梁阿龙
 * @date 2022-07-13 10:21
* */
@RestController
@RequestMapping("/ICT")
public class ICTransformation {



    @RequestMapping(value = "demo123" , method = RequestMethod.GET)
    public String demo123(int id){
        System.err.println(id);
        return "ok";
    }


    @PostMapping("/getICT")
    public String getICT(String s){
        String s1 = "";
        try {
            Integer i = Integer.valueOf(s);
            s1 = ITransformation(i);
        }catch (Exception e){
            s1 = CTransformation(s);
        }
        return s1;
    }


    /**
     * 方法说明: 数字转列号
     * @param integer
     * @return java.lang.String
     * @version v1.0
     * @author 梁阿龙
     * @date 2022-07-13 17:13*/
    public String ITransformation(Integer integer){
        int len = 0, power = 1, pre = 0;
        for (; pre < integer; pre += power) {
            power *= 26;
            len++;
        }
        char[] excelNum = new char[len];
        integer -= pre - power;
        integer--;
        for (int i = 0; i < len; i++) {
            power /= 26;
            excelNum[i] = (char) (integer / power + 'A');
            integer %= power;
        }
        return String.valueOf(excelNum);
    }


    /**
     * 方法说明: 列号转数字
     * @param s
     * @return java.lang.String
     * @version v1.0
     * @author 梁阿龙
     * @date 2022-07-13 17:14*/
    public String CTransformation(String s){
        char[] chars = s.toUpperCase(Locale.ROOT).toCharArray();
        int i = 0;
        for (char c : chars) {
            i = i * 26 + (c - 'A' + 1);
        }
        return String.valueOf(i);
    }
}
